	
********************************************************************************
*** Time shifts for synthetic controls
cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout

*********************************************************************
*** Fit treated on control leads/ lags to find best fit  
**** Need to do this for each of the treated

local StJoTreat = mofd(mdy(11,16,2004))
*local StJoSettle = mofd(mdy(12,21,2007))

local WheatonTreat = mofd(mdy(10,19,2004))
*local WheatonSettle = mofd(mdy(1,7,2010))

local ElCentroTreat = mofd(mdy(3,20,2006))
*local ElCentroSettle = mofd(mdy(9,20,2010))

local OverlookTreat = mofd(mdy(4,25,2008))
*local OverlookSettle = mofd(mdy(6,21,2012))

local MortonPlantTreat = mofd(mdy(8,27,2008)) 
*local MortonPlantSettle = mofd(mdy(11,20,2012))

local ShandsTreat = mofd(mdy(8,30,2008))
*local ShandsSettle = mofd(mdy(8,8,2013))

local chsTreat = mofd(mdy(1,7,2009))
*local chsSettle= mofd(mdy(8,4,2014))


local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {
	clear all 
	display "`dft'"

	** Controls handled separately as random sample

	** Keep only sample and defendant 
	use /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/op_ipop_controls_panel.dta, clear
	keep if prvdrnum == "`dft'"
	drop control
	append using "/homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/controls/`dft'_controls.dta"

	* Remove FE 
	reg pmt_amt i.dt 
	predict pmt_nofe, residuals
	drop pmt_amt
	rename pmt_nofe pmt_amt

	save /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout/op_ipop_panel_feout_`dft'.dta, replace

	*** Encode controls and treated variable
	encode(prvdrnum), gen(group)
	tsset group dt
	count if group == 999
	assert(r(N) == 0)
	replace group = 999 if prvdrnum == "`dft'"

	*** Save prvdrnum group crosswalk
	preserve
	keep prvdrnum group
	duplicates drop
	save "controls/`dft'_controlgroup_crosswalk.dta", replace
	restore


	*** Prep for looping leads-lags
	levelsof group, local(levels)
	keep group dt pmt_amt 
	reshape wide pmt_amt, i(dt) j(group)
	tsset dt
	mata: distanceLead= J(1,3,0)
	mata: distanceLag = J(1,3,0)
	* 3 columns: group, lag period, distance

	set matsize 11000


	** Only want to keep treated pre-period 
	local treatment_period = ``dft'Treat'
	replace pmt_amt999 = . if dt >= `treatment_period'


	** Find max lead so that there are 5 years of post-period data 
	summarize dt 
	local maxlead = r(max) - `treatment_period' + 1 - 60  
		** Max - treatment  + 1 = length of end window 
		** Take 60 off and that's the max you can shift before running out of data

	** Find max lag so that there are 3 years of pre-period data 
	summarize dt 
	local maxlag = max(`treatment_period' - r(min) - 36, 0)
		** Treatment period - min  = length of end window 
	

	*** Generate series of 0s for use in computing diff
	gen diff = 0 

	*** Loop, storing leads lags and fit
	foreach g of local levels {
		disp "Looping for defendant `dft', control `g'"
		* Skip treated group
		if `g' == 999{
				continue
			}

		foreach t of numlist 0/`maxlag'{
			*disp `t'
			qui replace diff = (pmt_amt999 - L`t'.pmt_amt`g')^2
			qui summarize diff 
			local dist = r(sum)/r(N)
			mata: distanceLag=  distanceLag \ `g', `t', `dist'
			}

		foreach t of numlist 0/`maxlead'{
			* disp `t'
			qui replace diff = (pmt_amt999 - F`t'.pmt_amt`g')^2
			qui summarize diff 
			local dist = r(sum)/r(N)
			mata: distanceLead=  distanceLead \ `g', `t', `dist'
			}
	}

	mata: st_matrix("distanceLead", distanceLead)
	mata: st_matrix("distanceLag", distanceLag)


	svmat distanceLag
	keep distanceLag*
	svmat distanceLead

	rename distanceLag1 laggroup
	rename distanceLag2 lagperiod
	rename distanceLag3 lagdistance
	rename distanceLead1 leadgroup
	rename distanceLead2 leadperiod
	rename distanceLead3 leaddistance

	drop if laggroup == 0
	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout
	save "distance/distancelaglead_`dft'.dta", replace
}

	********************************************************************************
	*** Find best fit per group

local dftlist "StJo Wheaton ElCentro Overlook MortonPlant Shands chs"
foreach dft of local dftlist {
	cd /homes/nber/jetson-dua52260/poterba-DUA52260/jetson-dua52260/ipop/substitutes/robustness/feout

	use "distance/distancelaglead_`dft'.dta", clear

	preserve
	keep laggroup lagperiod lagdistance
	drop if mi(laggroup)
	bysort laggroup: egen minlagdistance = min(lagdistance)
	keep if lagdistance == minlagdistance
	* Get rid of dupes
	collapse (min) lagperiod (min) lagdistance, by(laggroup)
	rename laggroup group
	save "distance/group_lag_mindistance_`dft'.dta", replace

	restore
	keep leadgroup leadperiod leaddistance
	drop if mi(leadgroup)
	bysort leadgroup: egen minleaddistance = min(leaddistance)

	keep if leaddistance == minleaddistance
	** Get rid of dupes 
	sort leadgroup leadperiod
	collapse (min) leadperiod (min) leaddistance, by(leadgroup)
	rename leadgroup group
	save "distance/group_lead_mindistance_`dft'.dta", replace

	clear
	use "distance/group_lag_mindistance_`dft'.dta"
	merge 1:1 group using "distance/group_lead_mindistance_`dft'.dta"

	drop _merge
	bysort group: gen mindistance = min(leaddistance, lagdistance)

	replace lagdistance = . if lagdistance!= mindistance
	replace leaddistance = . if leaddistance!= mindistance

	replace lagperiod = . if lagdistance == .
	replace leadperiod = . if leaddistance == .

	replace lagperiod = -1*lagperiod
	gen period = min(lagperiod, leadperiod)
		* Min because missing is infty 

	keep group period mindistance
	drop if mi(group)
	save "distance/groupleadlags_`dft'.dta", replace

}






